public with sharing class SelectMasterClientControllerTest {
	
	private static ID curAccId;
	private static ID curOppId;
	
	private static string recordTypeAASMasterClient   = RecordTypeHelper.GetRecordTypeId('Account', 'AAS Master Client');
    private static string recordTypeAASAgency         = RecordTypeHelper.GetRecordTypeId('Account', 'AAS Agency');
    private static string recordTypeAASSalesAccount   = RecordTypeHelper.GetRecordTypeId('Account', 'AAS Sales Account');
    private static string recordTypeAASIngenio	      = RecordTypeHelper.GetRecordTypeId('Account', 'Agency Managed Customer - Ingenio');
    private static string recordTypeLocalAdvertiser   = RecordTypeHelper.GetRecordTypeId('Account', 'Local OOR Customer');
    private static string AASOpportunityrecordTypeId  = RecordTypeHelper.GetRecordTypeId('Opportunity', 'Advanced Ad Solutions');
     private static string AASLeadrecordTypeId  = RecordTypeHelper.GetRecordTypeId('Lead', 'Advanced Ad Solutions');
	private static User userAAS;
	private static User userLocal,userDelAdmin;
    private static Account 	a_agency,a_mc,ma_sa;
    private static Lead l_convertlead;
	private static void init(){
		  string[] profileList = new String [] {'AAS Operations Team','Advanced Ad Solutions (Delegated Admin)','Advanced Ad Solutions (Client Services)','SFA OOR Sales Operations'};
		  list<Profile> pfs = [select ID, Name from Profile where Name  IN :profileList];
		  
	      //Create a map between the Profile Name and Id for easy retrieval
       	  Map<String,String> profiles = new Map<String,String>{};
     	  for(Profile pf: pfs)
        	profiles.put(pf.Name,pf.Id);
     
		  List<User> users = new List<User>();
	      userAAS = new User();
	      userAAS.UserName = 'AASUser@att.com';
	      userAAS.LastName = 'Test User1';
	      userAAS.Email = 'test1@attinteractive.com';
	      userAAS.Alias = 'testAAS' ;
	      userAAS.CommunityNickname = 'xyaz_1234556';
	      userAAS.TimeZoneSidKey = 'America/Los_Angeles';
	      userAAS.LocaleSidKey = 'en_US';
	      userAAS.EmailEncodingKey = 'UTF-8';
	      userAAS.ProfileId = profiles.get('AAS Operations Team');	      
	      userAAS.LanguageLocaleKey = 'en_US';
	      userAAS.IsActive = true;
	      //insert userAAS;
	      users.add(userAAS);
	      
	       userDelAdmin = new User();
	      userDelAdmin.UserName = 'AASDelAdminUser@att.com';
	      userDelAdmin.LastName = 'Test User3';
	      userDelAdmin.Email = 'test3@attinteractive.com';
	      userDelAdmin.Alias = 'testADM' ;
	      userDelAdmin.CommunityNickname = 'xyaz_1234558';
	      userDelAdmin.TimeZoneSidKey = 'America/Los_Angeles';
	      userDelAdmin.LocaleSidKey = 'en_US';
	      userDelAdmin.EmailEncodingKey = 'UTF-8';
	      userDelAdmin.ProfileId = profiles.get('Advanced Ad Solutions (Delegated Admin)');	      
	      userDelAdmin.LanguageLocaleKey = 'en_US';
	      userDelAdmin.IsActive = true;
	      //insert userDelAdmin;
	      users.add(userDelAdmin);
		
	      userLocal = new User();
	      userLocal.UserName = 'LocalUser@att.com';
	      userLocal.LastName = 'Test User2';
	      userLocal.Email = 'test2@attinteractive.com';
	      userLocal.Alias = 'testLOC' ;
	      userLocal.CommunityNickname = 'xyaz_1234557';
	      userLocal.TimeZoneSidKey = 'America/Los_Angeles';
	      userLocal.LocaleSidKey = 'en_US';
	      userLocal.EmailEncodingKey = 'UTF-8';
	      userLocal.ProfileId = profiles.get('SFA OOR Sales Operations');	      
	      userLocal.LanguageLocaleKey = 'en_US';
	      userLocal.IsActive = true;
	      //insert userLocal;
	      users.add(userLocal);
	      
	     
	      
	      
	      Account ma_agency,ma_mc;
	      System.runAs(userDelAdmin)	      
    	  {
    	  l_convertlead = new Lead();
	      l_convertlead.LastName = 'Test AAS Covert Lead ';
	      l_convertlead.LeadSource = 'Internet';
	      l_convertlead.Company = 'Test AAS Company';
	      l_convertlead.recordtypeId = AASLeadrecordTypeId;
	      insert l_convertlead;
	      
	       ma_agency = new Account(
                              name = 'Test Agency' ,
                              recordtypeid= recordTypeAASAgency ,
                              account_status__c = 'Draft'   ,
                              Phone= '510-123-1234'                                                        
                     ) ;
                     
	      insert ma_agency;
	    
           ma_mc = new Account(
                              name = 'Test Master Client',
                              recordtypeid=  recordTypeAASMasterClient,
                              account_status__c = 'Draft',
                              AAS_Agency_Name__c= ma_agency.Id
                     ) ;
          insert ma_mc;
           //test sales account
           ma_sa = new Account(
                              name = 'Test Sales account',
                              recordtypeid=  recordTypeAASSalesAccount,
                              account_status__c = 'Draft',
                              AAS_Agency_Name__c= ma_agency.Id,
                              Master_Client__c = ma_mc.id
                     ) ;
          insert ma_sa;
          
    	             
    	  }
    	  
    	  //retrieve the NASA ID.
	      a_agency= [Select id, name, NASA_ID__c from Account where id= :ma_agency.Id ];
		  System.Debug('*NASA->'+a_agency.NASA_ID__c);
		  a_mc = [Select id, name, NASA_ID__c from Account where id= :ma_mc.Id ];
		  System.Debug('*NASA->'+a_mc.NASA_ID__c);
	}
	
	 public static testMethod void testCreateAASSalesAccountWithNoOpportunity() {
		init();
				
		System.runAs(userDelAdmin)
    	{
			PageReference pageRef = Page.SelectMasterClient;
			Test.setCurrentPage(pageRef);
			
			//add recordtype parameters to URL
			ApexPages.currentPage().getParameters().put('RecordType', AASOpportunityrecordTypeId);
			ApexPages.currentPage().getParameters().put('retUrl', '/001');
			
			Opportunity opp = new Opportunity();
			ApexPages.StandardController sc = new ApexPages.StandardController(opp);
			
			//both selected agency and master clients are existing accounts.
			SelectMasterClientController controller = new SelectMasterClientController(sc);
		
			//controller.getselectionObject().setNewClientName('New Client');			
			controller.getselectionObject().setAgency(a_agency.Id);
			controller.getselectionObject().setClient(a_mc.Id);
			controller.getselectionObject().setAgencyId(a_agency.NASA_ID__c);
			controller.getselectionObject().setClientId(a_mc.NASA_ID__c);
			controller.getselectionObject().setAgencyName(a_agency.Name);
			controller.getselectionObject().setClientName(a_mc.Name);
			controller.getselectionObject().setCreateOpportunity('unchecked');
			PageReference nextPage = controller.createOpportunity();
			
			//check if AAS sales account was created.
			string nasaId=  a_agency.NASA_ID__c + '-' + a_mc.NASA_ID__c;
			Account acc= [select Id from Account where NASA_ID__c = : nasaId];
			System.assertNotEquals(acc, null);
			//redirected to standard create account page
			System.assert(nextPage.getUrl().Contains(acc.Id));
			
    	}
	}
	
	 public static testMethod void testCreateAASSalesAccountWithOpportunity() {
		init();
				
		System.runAs(userDelAdmin)
    	{
			PageReference pageRef = Page.SelectMasterClient;
			Test.setCurrentPage(pageRef);
			
			//add recordtype parameters to URL
			ApexPages.currentPage().getParameters().put('RecordType', AASOpportunityrecordTypeId);
			ApexPages.currentPage().getParameters().put('retUrl', '/001');
			
			Opportunity opp = new Opportunity();
			ApexPages.StandardController sc = new ApexPages.StandardController(opp);
			
			//both selected agency and master clients are existing accounts.
			SelectMasterClientController controller = new SelectMasterClientController(sc);
		
			//controller.getselectionObject().setNewClientName('New Client');			
			controller.getselectionObject().setAgency(a_agency.Id);
			controller.getselectionObject().setClient(a_mc.Id);
			controller.getselectionObject().setAgencyId(a_agency.NASA_ID__c);
			controller.getselectionObject().setClientId(a_mc.NASA_ID__c);
			controller.getselectionObject().setAgencyName(a_agency.Name);
			controller.getselectionObject().setClientName(a_mc.Name);
			controller.getselectionObject().setCreateOpportunity('checked');
			PageReference nextPage = controller.createOpportunity();
			
			//check if AAS sales account was created.
			string nasaId=  a_agency.NASA_ID__c + '-' + a_mc.NASA_ID__c;
			Account acc= [select Id from Account where NASA_ID__c = : nasaId];
			System.assertNotEquals(acc, null);
			
			Opportunity opp2= [select Id from Opportunity where Account.Id = :acc.Id];
			//check if opportunity for AAS sales account was created.
			System.assertNotEquals(opp2, null);
			string redirectUrl = '/' + opp2.Id; 
			
			//and page is redirected to the newly created opportunity
			System.assert(nextPage.getUrl().Contains(redirectUrl));
			
    	}
	}
	
	 public static testMethod void testConvertLead() {
		init();
				
		System.runAs(userDelAdmin)
    	{
			PageReference pageRef = Page.SelectMasterClient;
			Test.setCurrentPage(pageRef);
			
			//add recordtype parameters to URL
			ApexPages.currentPage().getParameters().put('RecordType', AASOpportunityrecordTypeId);
			ApexPages.currentPage().getParameters().put('retUrl', '/001');
			ApexPages.currentPage().getParameters().put('leadId',l_convertlead.Id);
			ApexPages.currentPage().getParameters().put('ent','lead');
			
			
			Opportunity opp = new Opportunity();
			ApexPages.StandardController sc = new ApexPages.StandardController(opp);
			
			//both selected agency and master clients are existing accounts.
			SelectMasterClientController controller = new SelectMasterClientController(sc);
		
			//controller.getselectionObject().setNewClientName('New Client');			
			controller.getselectionObject().setAgency(a_agency.Id);
			controller.getselectionObject().setClient(a_mc.Id);
			controller.getselectionObject().setAgencyId(a_agency.NASA_ID__c);
			controller.getselectionObject().setClientId(a_mc.NASA_ID__c);
			controller.getselectionObject().setAgencyName(a_agency.Name);
			controller.getselectionObject().setClientName(a_mc.Name);
			controller.getselectionObject().setCreateOpportunity('checked');
			PageReference nextPage = controller.createOpportunity();
			
			//check if AAS sales account was created.
			string nasaId=  a_agency.NASA_ID__c + '-' + a_mc.NASA_ID__c;
			Account acc= [select Id from Account where NASA_ID__c = : nasaId];
			System.assertNotEquals(acc, null);
			//redirected to opportunity  page
			System.assert(nextPage.getUrl().Contains('/006'));
			
    	}
	}
	
	 public static testMethod void testConvertLeadWithNoOpportunity() {
		init();
				
		System.runAs(userDelAdmin)
    	{
			PageReference pageRef = Page.SelectMasterClient;
			Test.setCurrentPage(pageRef);
			
			//add recordtype parameters to URL
			ApexPages.currentPage().getParameters().put('RecordType', AASOpportunityrecordTypeId);
			ApexPages.currentPage().getParameters().put('retUrl', '/001');
			ApexPages.currentPage().getParameters().put('leadId',l_convertlead.Id);
			ApexPages.currentPage().getParameters().put('ent','lead');
			
			
			Opportunity opp = new Opportunity();
			ApexPages.StandardController sc = new ApexPages.StandardController(opp);
			
			//both selected agency and master clients are existing accounts.
			SelectMasterClientController controller = new SelectMasterClientController(sc);
		
			//controller.getselectionObject().setNewClientName('New Client');			
			controller.getselectionObject().setAgency(a_agency.Id);
			controller.getselectionObject().setClient(a_mc.Id);
			controller.getselectionObject().setAgencyId(a_agency.NASA_ID__c);
			controller.getselectionObject().setClientId(a_mc.NASA_ID__c);
			controller.getselectionObject().setAgencyName(a_agency.Name);
			controller.getselectionObject().setClientName(a_mc.Name);
			controller.getselectionObject().setCreateOpportunity('unchecked');
			PageReference nextPage = controller.createOpportunity();
			
			//check if AAS sales account was created.
			string nasaId=  a_agency.NASA_ID__c + '-' + a_mc.NASA_ID__c;
			Account acc= [select Id from Account where NASA_ID__c = : nasaId];
			System.assertNotEquals(acc, null);
			//redirected to opportunity  page
			System.assert(nextPage.getUrl().Contains('/001'));
			
    	}
	}
	
	 public static testMethod void testAASAccountRedirect() {
		init();
				
		System.runAs(userDelAdmin)
    	{
    		
    		PageReference pageRef = Page.SelectMasterClient;
			Test.setCurrentPage(pageRef);
			ApexPages.currentPage().getParameters().put('retUrl', '/001');
			ApexPages.currentPage().getParameters().put('accountId', ma_sa.Id);
		
			Opportunity opp = new Opportunity();
			ApexPages.StandardController sc = new ApexPages.StandardController(opp);
			
			//both selected agency and master clients are existing accounts.
			SelectMasterClientController controller = new SelectMasterClientController(sc);
			PageReference nextPage =controller.Init(); 
			
			//redirected to opportunity  page
			//System.assert(nextPage.getUrl().Contains('/006/e'));
    	
	 }
    	}
}